package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.service.ModelgroupService;
import sjtu.ist.warehouse4ws.vo.ModelgroupVO;
import sjtu.ist.warehouse4ws.vo.VersionVO;

import com.opensymphony.xwork2.ActionSupport;

public class ModelgroupAction extends ActionSupport{
	private ModelgroupService modelgroupService;
	RepositoryMoudl repositoryMoudlService;
	private List<ModelgroupVO> modelgroupList = new ArrayList<ModelgroupVO>();
	private List<VersionVO> versionList = new ArrayList<VersionVO>();
	
	private String groupName;
	
	private boolean success=true;
	
	private int start;
	private int limit;
	private int count;

	public String showModelgroupList(){				
		modelgroupList=modelgroupService.getAll();
		return SUCCESS;
	}
	
	public String showVersionList(){			
		Subject cuser=SecurityUtils.getSubject();
		String companyId=cuser.getSession().getAttribute("companyId").toString();
		List<String> versions=repositoryMoudlService.getVersions(companyId, String.valueOf(groupName));	
		for(String version : versions){
			VersionVO vv=new VersionVO(version);
			versionList.add(vv);
		}		
		
		return SUCCESS;
	}
	
	@JSON(name = "totalModelgroup")
	public int getModelgroup() {
		return this.modelgroupList.size();
		//return count;
	}

	@JSON(name = "Modelgroup")
	public List<ModelgroupVO> getModelgroupList() {
		return this.modelgroupList;
	}
	
	@JSON(name = "totalVersion")
	public int getVersion() {
		return this.versionList.size();
		//return count;
	}

	@JSON(name = "version")
	public List<VersionVO> getVersionList() {
		return this.versionList;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setModelgroupService(ModelgroupService modelgroupService) {
		this.modelgroupService = modelgroupService;
	}
	
	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService){
		this.repositoryMoudlService = repositoryMoudlService;
	}
}